<?php

namespace app\api\model;

use think\Model;

/**
 * 邮箱验证码
 */
class Upload
{

    public static function uploadImages($files, $validate = [], $subDir = '')
    {

        // 检查是否有文件上传
        if ($files) {
            if (!is_array($files)) {
                $files = [$files];
            }

            foreach ($files as $file) {
                // 验证文件，如文件类型、大小等
                $defaultValidate = [
//                    'size' => 1024 * 1024 * 2, // 2MB
                    'ext'  => 'jpg,jpeg,png,gif',
                ];
                $validate = array_merge($defaultValidate, $validate);

                $info = $file->validate($validate)->move(realpath($_SERVER['argv'][0]).DS.'uploads'.DS);

                if ($info) {
                    // 文件上传成功
                    $imagePaths = config('app.image_url').'/uploads' . DS . $subDir  . $info->getSaveName();
                } else {
                    return ['code' => 201, 'msg' => $file->getError()];
                }
            }
        }
        if($imagePaths){
            return ['code' => 200, 'msg' => '获取信息成功', 'data' => $imagePaths];
        }
        return ['code' => 201, 'msg' => '上传失败'];

    }
}
